package com.buddy.sds.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buddy.sds.auth.entity.DepartmentTree;
import com.buddy.sds.auth.entity.SystemDepartment;
import com.buddy.sds.auth.mapper.ISystemDepartmentMapper;
import com.buddy.sds.auth.service.ISystemDepartmentService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Primary
public class SystemDepartmentServiceImpl extends ServiceImpl<ISystemDepartmentMapper, SystemDepartment> implements ISystemDepartmentService {

    private static String TOP_DEPARTMENT_ID = "ROOT" ;

    @Override
    public List<DepartmentTree> getDepartmentTree() {
        //部门根节点为ROOT
        return getDepartmentTree(TOP_DEPARTMENT_ID);
    }

    @Override
    public boolean idExists(String did) {
        return baseMapper.idExists(did);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteCascase(String depid) {
        List<SystemDepartment> departments = baseMapper.getDepartmentListByPid(depid);

        //删除子节点
        departments.forEach(d->{
            deleteCascase(d.getDepartmentId());
        });

        //删除自己
        baseMapper.deleteById(depid);
        //清空关联用户的部门信息
        baseMapper.emptyUserDep(depid);

    }

    /**
     * 判断2是不是1的后代节点
     * @param departmentId1
     * @param departmentId2
     * @return
     */
    @Override
    public boolean isChildNode(String departmentId1, String departmentId2) {

        //直接从2向上查找父节点,判断是否包含1即可

        SystemDepartment d2 = baseMapper.selectById(departmentId2);

        String d2pid = d2.getDepartmentPid();

        if(TOP_DEPARTMENT_ID.equals( d2pid )){

            //如果父节点是ROOT d2节点就是根节点
            return false;
        }else if(d2pid.equals(departmentId1)){
            //父子关系匹配上了
            return true;
        }else{
            //么有匹配上，继续向上一层查找
            return isChildNode(departmentId1,d2pid);
        }
    }

    @Override
    public SystemDepartment getTopDeptByCurrentDept(SystemDepartment currentDept) {
        if(TOP_DEPARTMENT_ID.equals(currentDept.getDepartmentPid())){return currentDept ;}

        SystemDepartment upDept = baseMapper.getDepartmentById(currentDept.getDepartmentPid()) ;
        if(upDept != null){
            if(TOP_DEPARTMENT_ID.equals(upDept.getDepartmentPid())){
                return upDept ;
            }else {
                return getTopDeptByCurrentDept(upDept);
            }
        }
        return null;
    }

    private List<DepartmentTree> getDepartmentTree(String pid){

        List<SystemDepartment> departments = baseMapper.getDepartmentListByPid(pid);

        List<DepartmentTree> departmentTrees = new ArrayList<>();

        departments.forEach(d->{

            DepartmentTree departmentTree = new DepartmentTree();

            departmentTree.setDepartment(d);

            departmentTree.setChildren(getDepartmentTree(d.getDepartmentId()));

            departmentTrees.add(departmentTree);
        });

        return departmentTrees;
    }

}
